package com.ftg.learn.bus.role.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.basicinfo_record.service.BasicInfoRecordService;
import com.ftg.learn.bus.control_list.service.control_list_service;
import com.ftg.learn.bus.role.entity.Role;
import com.ftg.learn.bus.role.mapper.RoleMapper;
import com.ftg.learn.bus.userole_control.service.userole_control_service;
import com.ftg.learn.bus.util.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    userole_control_service ucs;

    @Autowired
    control_list_service cls;

    @Autowired
    BasicInfoRecordService birs;


    /**
     * 查询所有角色
     * @return
     */
    public List<Role> showAll(){
        QueryWrapper<Role> qw = new QueryWrapper<>();
        return roleMapper.selectList(qw);
    }

    /**
     * 查询一条
     * @return id
     */
    public Role findOne(String id){
        return roleMapper.selectById(id);
    }

    /**
     * 添加一条角色数据
     * @return role
     */
    public int insert(Role role){
        if(role.getRole_id()==null||role.getRole_id().equals("") || role.getRole_name()==null||role.getRole_name().equals("")){
            return -1;
        }
        if(roleMapper.selectById(role.getRole_id()) != null){
            return -1;
        }
        birs.BasicInsertRoleRecord(role.getRole_id(),role.getRole_name());
        return roleMapper.insert(role);

    }

    /**
     * 修改一条角色数据
     * @return role
     */
    public int update(Role role) {
        if(role.getRole_id() == null || role.getRole_id().equals("") || role.getRole_name() == null || role.getRole_name().equals("")){
            return -1;
        }
        String id = role.getRole_id();
        String name = role.getRole_name();
        birs.BasicUpdateRoleRecord(id,name);
        return roleMapper.update(role);

    }

    /**
     * 删除一条角色数据
     * @return id
     */
    public int delete(String role_id){
        if(findOne(role_id)!=null){
            ucs.deletByRole_id(role_id);
            cls.delete(role_id);
            return roleMapper.delete(role_id);
        }else {
            return -1;
        }
    }

    /**
     * 根据角色id  删除多角色
     * @param role_ids
     */
    public int deletes(List<String> role_ids) {
        if(role_ids ==null || role_ids.size()==0){
            return 0;
        }
        roleMapper.deleteForUserole_controlByrole_ids(role_ids);

        List<String> userids=roleMapper.selectUser_id(role_ids);
        if( userids.size()!=0){
            roleMapper.deleteForUserole_controlByuser_ids(userids);
        }
        return roleMapper.deleteBatchIds(role_ids);
    }

    /**
     * 分页查询
     * @param page          总页数
     * @param limit         每页显示条数
     * @param role_id       角色id
     * @param role_name     角色名称
     * @return
     */
    public ResultType<Role> query(int page, int limit,String role_id,String role_name){
        ResultType<Role> rt = new ResultType<>();
        rt.setCode(0);
        IPage<Role> ip = new Page<>(page,limit);
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.and(wrapper -> wrapper.like("role_id",role_id).like("role_name",role_name));
        ip =  roleMapper.selectPage(ip,qw);
        rt.setData(ip.getRecords());
        rt.setCount(ip.getTotal());
        return rt;
    }
}
